<?php

/**
 * Class BaseRestaurantApiService 食天一隅 订餐接口服务层
 */
class BaseRestaurantApiService extends BaseService
{
    private $mOrder;
    private $token;

    public function __construct()
    {
        $this->mOrder = D('Base/RestaurantstyyOrder');
        $this->token = M('Token');
    }

    /***
     * 发送订单信息给校联
     */
    public function sendOrder($orderId){
        $token = RedisService::get('xiaolian_token_time');
        if(!$token){
            $this->token();
            $token = RedisService::get('xiaolian_token_time');
        }
        $token = substr($token['data'],strpos($token['data'],':')+2);
        //查询订单信息
        $data = $this->mOrder->relation(true)->getByPk($orderId);
        $prods = '';
        foreach($data['RestaurantOrderGoods'] as $k=>$v){
            $prods .= '1'.','.$v['goods_id'].','.$v['goods_name'].','
                .$v['goods_price'].','.$v['goods_price'].','.$v['category_id'].',0'.',0'.' ';
        }
        $prods = substr($prods,0,strlen($prods)-1);
//        $url = "https://api2.zjxlxx.com/p/payment.xl?token=".$token."&userid=".$data['user_id']."&seq=".$data['order_sn']."
//        &selldate=".date('Y-m-d H:i:s',$data['pay_time'])."&offline=".$data['is_same']."&money=".$data['order_amount']."&windowid=0&rfid=110&cid=110&sno=110
//        &account=".$data['card_number']."&prods=".$prods;

        $info = array(
            'token'=> $token,
            'userid'=> $data['user_id'],
            'seq'=> $data['order_sn'],
            'selldate'=> date('Y-m-d H:i:s',$data['pay_time']),
            'offline'=> $data['is_same'],
            'money'=> $data['order_amount'],
            'windowid'=> 0, //餐台id
            'rfid'=> 0,  //卡号
            'cid'=> 0,   //校园卡芯片编号
            'sno'=> 0,   //校园卡统一编号
            'account'=> $data['card_number'], //校园卡帐号
            'prods' => $prods
        );
        $url = http_build_query($info);
        $url = "http://smart.zjulab.com:82/p/payment.xl?".$url;
        $res = $this->curl($url);
        wlog(date("Y-m-d H:i:s")."data:orderId".'-------------------------------------',"send");
        wlog(date("Y-m-d H:i:s")."data:orderId".var_export($orderId,true),"send");
        wlog(date("Y-m-d H:i:s")."data:res".var_export($res['msg'],true),"send");
        if($res['code']==200){
            return 1;
        }else{
            return 0;
        }
    }


    /**
     * 获取token
     */
    public function token()
    {
        $user = 'CzTest';
        $pwd = '123456';
        $m_code = '02170307';
        $time = (int)microtime(true)*1000;
        $request = array(
            'usr' => $user,
            'pwd' => $pwd,
            'm_code' => $m_code,
            'timestamp'=> $time,
        );
        $sign = $this->sign($request);
        $url = 'http://smart.zjulab.com:82/u/login.xl?timestamp='.$time.'&usr=CzTest&pwd=123456&m_code='.$m_code.'&sig='.$sign;
        $token = $this->curl($url);
        RedisService::set('xiaolian_token_time', $token,120);
    }

    public function user($user){
        return base64_encode($user);
    }

    public function pwd($pwd){
        return strrev(md5($pwd));
    }

    public function sign($params){
        ksort($params);
        $signStr = '';
        foreach($params as $k=>$v){
            $signStr .= $k . '=' . $v;
        }
        $signStr .= '&eO$M]@Y,kCBi~(I';
        return md5($signStr);
    }

    /**
     * http请求
     * @param $url 请求地址
     * @param null $postFields 请求参数
     * @return mixed
     * @throws Exception
     */
    public function curl($url = '', $postParams = null)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        //超时时间
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        // post提交数据
        curl_setopt($ch, CURLOPT_POST, 1);
        // post的变量
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postParams);
        if (!is_array($postParams)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(

                'Content-Type: application/json',
                'Content-Length: ' . strlen($postParams)
            ));
        }
        $output = json_decode(curl_exec($ch), true);
        curl_close($ch);
        //打印获得的数据
        return $output;
    }

}